package com.wzapp.sitposture.task.service.impl;

import com.wzapp.sitposture.task.entity.Task;
import com.wzapp.sitposture.task.repository.TaskRepository;
import com.wzapp.sitposture.task.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by qianglong on 2018/1/22.
 */
@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    TaskRepository taskRepository;

    /**
     * cycle-重复执行，immediately-立即执行一次，scheduled-预定时间执行一次，默认immediately
     */
    private final String DEFAULT_TYPE = "immediately";

    /**
     * 状态：0-新建，1-开始执行，2-完成，3-取消，4-失败
     */
    private final Integer DEFAULT_STATUS = 0;

    @Override
    public Task saveTask(Task task) {

        task.setType(DEFAULT_TYPE);
        task.setStatus(DEFAULT_STATUS);
        task.setCreateTime(System.currentTimeMillis());
        task.setUpdateTime(System.currentTimeMillis());
        return taskRepository.save(task);
    }

    @Override
    public Task findByCreatorAndName(String creator, String name) {

        return taskRepository.findByCreatorAndNameAndStatus(creator,name,DEFAULT_STATUS);
    }

}